package com.apobates.forum.toro.core;

import java.util.HashSet;
import java.util.Set;

/**
 * 布尔运算符枚举
 * 支持: 或(OR), 非(NOT), 与(AND)
 */
public enum BoolLogicType {
    AND("&", "&&"),
    OR("|", "||"),
    NOT("!", "!");
    // 表达式中使用的
    private final String symbol;
    // 最终的运算符
    private final String operator;

    BoolLogicType(String symbol, String operator) {
        this.symbol = symbol;
        this.operator = operator;
    }

    public String getSymbol() {
        return symbol;
    }

    public String getOperator() {
        return operator;
    }

    /**
     *
     * @param names
     * @return
     */
    public static BoolLogicType getInstance(String names) {
        BoolLogicType ins = null;
        for(BoolLogicType ele:values()){
            if(ele.name().toLowerCase().equals(names.toLowerCase())){
                ins = ele;
                break;
            }
        }
        return ins;
    }

    /**
     *
     * @param symbol
     * @return
     */
    public static BoolLogicType getInstanceBySymbol(String symbol) {
        BoolLogicType ins = null;
        for(BoolLogicType ele:values()){
            if(ele.getSymbol().toLowerCase().equals(symbol.toLowerCase())){
                ins = ele;
                break;
            }
        }
        return ins;
    }

    /**
     *
     * @return
     */
    public static Set<String> getAllowSymbol(){
        Set<String> data = new HashSet<>();
        for(BoolLogicType ele:values()){
            data.add(ele.getSymbol());
        }
        return data;
    }
}
